<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <Tree :node-data="company" :root="root" @adddept="adddept" />
        <!-- <el-tree :data="departs" :props="defaultProps" default-expand-all /> -->
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 用了一个行列布局 -->
          <template #default="{data}">
            <Tree :node-data="data" @del-dept="getD" @adddept="adddept" />

          </template>
        </el-tree>

      </el-card>

    </div>

    <Adddept
      :is-show="isShow"
      :depts-list="deptsList"
      :node-data="nodeData"
      @handleClose="handleClose"
      @getD="getD"
    />
  </div>
</template>

<script>
// import Tree from '@/views/departments/tree.vue'
import Tree from '@/views/departments/components/tree-tools.vue'
import { reqGetDepartments } from '@/api/departments'
import { fn } from '@/utils/index'
import Adddept from '@/views/departments/components/add-dept.vue'
export default {

  name: 'Departments',
  components: {
    Tree,
    Adddept
  },
  data() {
    return {
      isShow: false,
      root: true,
      company: { name: '船只剥壳', manager: '黑马' },
      departs: [],
      nodeData: {},
      deptsList: [],
      // flag: '',
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getD()
  },
  methods: {
    async getD() {
      const { data: { companyName, depts }} = await reqGetDepartments()
      console.log(companyName, depts, 'abababab')
      this.company.name = companyName
      this.departs = fn(depts, '')
      this.deptsList = depts
      console.log(fn(depts, ''))
      console.log('触发了删除操作')

      // console.log(fn(depts, ''), '大啊哒哒哒啊')
    },
    handleClose() {
      this.isShow = false
      // console.log(123123)
    },
    adddept(qwer) {
      this.isShow = true
      this.nodeData = qwer
      console.log(qwer, 999999999)
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
